package com.example.newapplication.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MyLineChart extends View {

    private final String TAG = MyLineChart.class.getSimpleName();

    public MyLineChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAction();
    }

    private Paint paintYItem; // 专门用于画Y轴的画笔
    private Paint paintXItem; // 专门用于话X轴的画笔

    private Paint paintLine;  // 线与点共用一个画笔

    private Rect rectX = new Rect();
    private Rect rectY = new Rect();

    private int w;
    private int h;

    private String[] xItemText = {}; // X轴的文字
    private String[] yItemText = {}; // Y轴的文字

    /**
     * 给使用者设置X轴的文字集合
     */
    public void setXItemText(String[] xItemText) {
        this.xItemText = xItemText;
    }

    /**
     * 给使用者设置Y轴的文字集合
     */
    public void setYItemText(String[] yItemText) {
        this.yItemText = yItemText;
        saveYHeightList = new int[yItemText.length];
    }

    private void initAction() {
        paintYItem = new Paint();
        paintYItem.setAntiAlias(true); // 抗锯齿
        paintYItem.setColor(Color.RED);
        paintYItem.setTextSize(30);

        paintXItem = new Paint();
        paintXItem.setAntiAlias(true);
        paintXItem.setColor(Color.BLUE);
        paintXItem.setTextSize(30);

        paintLine = new Paint();
        paintLine.setAntiAlias(true);
        paintLine.setColor(Color.BLACK);
        paintLine.setTextSize(30);

    }

    /**
     * 测量自身View控件的宽和高，宽和高得到父控件给子控件测量的高宽等
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        // 精确模式
        // MeasureSpec.EXACTLY 当控件设置尺寸的值是多少，控件的宽高就是多少

        // 最大模式
        // MeasureSpec.AT_MOST 由父控件给出了最大控件，子控件最大只能这么大，当然也可以比父控件小

        if (widthMode == MeasureSpec.EXACTLY) {
            w = widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            h = heightSize;
        }

        if (heightMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.AT_MOST) {
            throw new IllegalArgumentException("参数异常，请输入指定大小，例如：200dp");
        }

        setMeasuredDimension(w, h);
    }

    private int countYHeight; // 计算记录Y的高度

    private int[] saveYHeightList; // 保存Y轴 五级的高度

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (yItemText.length == 0 || xItemText.length == 0) {
            throw new IllegalArgumentException("参数异常，设置x轴/y轴集合数据值为空");
        }

        // 画Y轴集合数据
        for (int y = 0; y< yItemText.length; y++) {
            countYHeight = (y + 1) * 85;
            canvas.drawText(yItemText[y], 40, countYHeight, paintYItem);
            saveYHeightList[y] = countYHeight;
        }

        paintLine.getTextBounds(xItemText[0], 0, xItemText[0].length(), rectX);
        paintLine.getTextBounds(yItemText[0], 0, yItemText[0].length(), rectY);

        // 画X轴集合的数据
        for (int x = 0; x <xItemText.length; x++) {
            int xLocation = (x + 1) * 110;
            canvas.drawText(xItemText[x], xLocation, (countYHeight + 80), paintXItem);

            // 定义线的 开始 到 结束
            int tempStartY = 0;
            int tempStopY = 0;
            switch (x) {
                case 0:
                    tempStartY = saveYHeightList[0] - (rectY.height() / 1);
                    tempStopY = saveYHeightList[1] - (rectY.height() / 1);
                    break;
                case 1:
                    tempStartY = saveYHeightList[1] - (rectY.height() / 1);
                    tempStopY = saveYHeightList[2] - (rectY.height() / 1);
                    break;
                case 2:
                    tempStartY = saveYHeightList[2] - (rectY.height() / 1);
                    tempStopY = saveYHeightList[3] - (rectY.height() / 1);
                    break;
                case 3:
                    tempStartY = saveYHeightList[3] - (rectY.height() / 1);
                    tempStopY = saveYHeightList[4] - (rectY.height() / 1);
                    break;
                case 4:
                    tempStartY = saveYHeightList[4] - (rectY.height() / 1);
                    tempStopY = saveYHeightList[3] - (rectY.height() / 1);
                    break;
                case 5:
                    tempStartY = saveYHeightList[3] - (rectY.height() / 1);
                    tempStopY  = saveYHeightList[1] - (rectY.height() /1);
                    break;
                case 6:
                    tempStartY = saveYHeightList[1] - (rectY.height() / 1);
                    break;
                default:
                    break;
            }

            // 画点，注意：X轴需要与点保持一致的位置，只变化Y轴值的位置
            canvas.drawCircle(xLocation + (rectX.width() / 2),
                    tempStartY,
                    10,
                    paintLine);

            // 画线
            if (x < xItemText.length-1) {
                int xLocationLine = ((x + 1) + 1) * 110;
                canvas.drawLine(xLocation + (rectX.width() / 2),
                        tempStartY,
                        xLocationLine + (rectX.width() / 2),
                        tempStopY,
                        paintLine);
            }
        }
    }
}
